<?php
class ControllerMessageLostProperty extends Controller {
	
	public function init(){
		$this->language->load('message/lost_property');
		$this->load->model('message/lost_property');
		$this->load->model('tool/image');
	}
	
	public function index() {
		$this->init();
		
		$this->getList();
	}
	
	protected  function getList(){
		$this->data['breadcrumbs'] = array();
		
		$this->document->setTitle('失物招领');	
		$this->data['heading_title'] 	 = '失物招领';
		$this->data['lostProperties']  = array();
		
		if(isset($this->request->post['keyword'])){
			$keyword = $this->request->post['keyword'];
		}else{
			$keyword = null;
		}
		
		$data = array(
			'keyword'=>$keyword
		);
		
		$results= $this->model_message_lost_property->getAllProperties($data);
		$this->data['search'] = $this->url->link('message/lost_property','','SSL');
		
		foreach ($results as $lostProperty) {
				if ($lostProperty['pic_url'] && file_exists(DIR_IMAGE . $lostProperty['pic_url'])) {
					$image = $this->model_tool_image->resize($lostProperty['pic_url'], 100, 80);
				} else {
					$image = $this->model_tool_image->resize('no_image.jpg', 100, 80);
				}
				$this->data['lostProperties'][] = array(
					'image' =>$image,
					'name' =>$lostProperty['name'],
					'summary' =>$lostProperty['summary'],
					'date_added' =>date('Y-m-d', strtotime($lostProperty['date_added'])),
					'href'=>$this->url->link('message/lost_property/info', 'lost_property_id='.$lostProperty['lost_property_id'], 'SSL'),
				);
			}
		
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/lost_properties_list.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/message/lost_properties_list.tpl';
		} else {
			$this->template = 'default/template/message/lost_properties_list.tpl';
		}

		$this->data['keyword'] = $keyword;
		$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
				);

		$this->response->setOutput($this->render());
	}
	
	
	public function info(){
		$this->init();
		
		$this->data['breadcrumbs'] = array();
		
		$this->document->setTitle('失物招领');	
		$this->data['heading_title'] 	 = '失物招领';
		
		if(isset($this->request->get['lost_property_id'])){
			$lost_property_id = $this->request->get['lost_property_id'];
		}else{
			$lost_property_id = 0;
		}
		
		$data = array(
			'lost_property_id'=>$lost_property_id
		);
		
		$result = $this->model_message_lost_property->getProperty($data);
		
		if($result){
			if ($result['pic_url'] && file_exists(DIR_IMAGE . $result['pic_url'])) {
				$image = $this->model_tool_image->resize($result['pic_url'], 100, 80);
			} else {
				$image = $this->model_tool_image->resize('no_image.jpg', 100, 80);
			}
			
			$this->load->model('message/police');	

			$this->data['polices'] = $this->model_message_police->getAllPolice($result['community_id']);
			
			
			
			$this->data['lostProperty'] = array(
				'image' =>$image,
				'name' =>$result['name'],
				'title' =>$result['title'],
				'summary'=>$result['summary'],
				'description' =>html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'),
				'href'=>$this->url->link('message/lost_property/info', 'lost_property_id='.$result['lost_property_id'], 'SSL'),
			);
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/lost_property_info.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/message/lost_property_info.tpl';
		} else {
			$this->template = 'default/template/message/lost_property_info.tpl';
		}

		$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
				);

		$this->response->setOutput($this->render());
		
		
	}
	
	private function redirectToList(){
		$this->session->data['success'] = sprintf($this->language->get('text_success'),$this->language->get('heading_title'));

		$this->redirect(HTTPS_SERVER . 'index.php?route=message/order');
	}
}
?>